package console;

import com.apple.eawt.AboutHandler;
import com.apple.eawt.AppEvent.AboutEvent;
import com.apple.eawt.Application;
import editor.WITNAEditor;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Michael Hanns
 *
 */
public class ClientLauncher {

    private final double softwareVersion = 1.3;

    protected void startupWindows(boolean edit) {
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientLauncher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ClientLauncher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ClientLauncher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ClientLauncher.class.getName()).log(Level.SEVERE, null, ex);
        }

        Runnable splash = new Splash();
        new Thread(splash).start();
        if (edit) {
            final WITNAEditor editor = new editor.windows.WITNAFrameView(true, softwareVersion);
            editor.setVisible(true);
            ((Splash) splash).stopSplash();
            editor.toFront();
        } else {
            ClientObserver client = new ClientObserver(softwareVersion, false);
            ((Splash) splash).stopSplash();
            client.launchGUI();
        }
    }

    protected void startupOSX(boolean edit) {
        Runnable splash = new Splash();
        new Thread(splash).start();

        Application application = Application.getApplication();
        Image image = Toolkit.getDefaultToolkit().getImage("img/witnalogo.png");
        application.setDockIconImage(image);

        if (edit) {
            final WITNAEditor editor = new editor.osx.WITNAFrameView(true, softwareVersion);
            editor.setVisible(true);
            application.setAboutHandler(new AboutHandler() {
                @Override
                public void handleAbout(AboutEvent ae) {
                    new console.gui.osx.About((Frame) editor, true, softwareVersion).setVisible(true);
                }
            });
            ((Splash) splash).stopSplash();
            editor.toFront();
        } else {
            final ClientObserver client = new ClientObserver(softwareVersion, true);
            application.setAboutHandler(new AboutHandler() {
                @Override
                public void handleAbout(AboutEvent ae) {
                    new console.gui.osx.About((Frame) client.getGUI(), true, softwareVersion).setVisible(true);
                }
            });
            ((Splash) splash).stopSplash();
            client.launchGUI();
        }
    }

    protected void startupCmd() {
        System.out.println("Loading..\n");
        CommandLineClient client = new CommandLineClient(softwareVersion);
        client.takeInput();
    }

    public static void main(String[] args) {
        boolean osx = System.getProperty("os.name").contains("OS X");
        boolean editor = false;
        boolean cmdLine = false;
        for (int x = 0; x < args.length; x++) {
            if (args[x].equals("-edit")) {
                editor = true;
            } else if (args[0].equals("-cmd")) {
                cmdLine = true;
            }
        }

        if (cmdLine) {
            new ClientLauncher().startupCmd();
        } else if (osx) {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "WITNA");
            new ClientLauncher().startupOSX(editor);
        } else {
            new ClientLauncher().startupWindows(editor);
        }
    }
}
